package cn.lili.modules.order.cart.render.impl;

import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.modules.order.cart.entity.dto.TradeDTO;
import cn.lili.modules.order.cart.entity.enums.RenderStepEnums;
import cn.lili.modules.order.cart.entity.vo.CartSkuVO;
import cn.lili.modules.order.cart.render.CartRenderStep;
import cn.lili.modules.promotion.client.ReturnActivityClient;
import cn.lili.modules.promotion.entity.dos.ReturnActivityUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * 渲染我的全返卷实现
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ReturnActivityRender implements CartRenderStep {

    private final ReturnActivityClient returnActivityClient;

    @Override
    public RenderStepEnums step() {
        return RenderStepEnums.RETURN_ACTIVITY;
    }

    @Override
    public void render(TradeDTO tradeDTO) {
        //全返卷列表
        renderReturnActivity(tradeDTO);
        //计算优惠价格
        renderPrice(tradeDTO);
    }

    /**
     * 查询全返卷列表
     *
     * @param tradeDTO 交易dto
     */
    private void renderReturnActivity(TradeDTO tradeDTO) {
        if (null != tradeDTO.getPromotionType()) {
            return;
        }

        // 免税商品不能使用优惠券
        if (StringUtils.isNotEmpty(tradeDTO.getAuthName()) && StringUtils.isNotEmpty(tradeDTO.getAuthCardNo())) {
            return;
        }

        boolean isMh = false;
        boolean isGreenSecurities = false;
        for (CartSkuVO cartSkuVO : tradeDTO.getSkuList()) {
            if (StringUtils.isNotEmpty(cartSkuVO.getGoodsSku().getMhId())) {
                isMh = true;
                break;
            }
            // 判断有没有全返商品， 没有全返商品不能使用全返券
            if (null != cartSkuVO.getGoodsSku().getIsGreenSecurities() && cartSkuVO.getGoodsSku().getIsGreenSecurities()) {
                isGreenSecurities = true;
                break;
            }


        }
        if (isMh || !isGreenSecurities) {
            return;
        }

        List<ReturnActivityUser> list = returnActivityClient.getList(tradeDTO.getMemberId());
        tradeDTO.setReturnActivityUserList(list);
    }

    /**
     * 计算优惠价格
     */
    private void renderPrice(TradeDTO tradeDTO) {
        //选择的全返卷
        ReturnActivityUser returnActivityUser = tradeDTO.getReturnActivityUser();
        if(returnActivityUser == null){
            return;
        }

        Double returnPrice = returnActivityUser.getPrice();
        for (CartSkuVO cartSkuVO : tradeDTO.getSkuList()) {
            // 非全返商品跳过
            if (!cartSkuVO.getIsGreenSecurities()) {
                continue;
            }
            // 商品数量 * 商品最大抵扣金额 = 可抵扣金额
            Double greenSecuritiesPrice = CurrencyUtil.mul(cartSkuVO.getIsGreenSecuritiesPrice(), cartSkuVO.getNum());

            // 判断商品可抵扣金额是否大于全返券金额
            if (greenSecuritiesPrice >= returnPrice) {
                cartSkuVO.getPriceDetailDTO().setReturnActivityUserPrice(returnPrice);
                returnPrice = CurrencyUtil.sub(returnPrice, greenSecuritiesPrice);
                break;
            }else {
                returnPrice = CurrencyUtil.sub(returnPrice, greenSecuritiesPrice);
                cartSkuVO.getPriceDetailDTO().setReturnActivityUserPrice(greenSecuritiesPrice);
            }
        }

        // 计算全返券抵扣金额
        if (returnPrice <= 0) {
            tradeDTO.setReturnActivityPrice(returnActivityUser.getPrice());
        }else {
            tradeDTO.setReturnActivityPrice(CurrencyUtil.sub(returnActivityUser.getPrice(), returnPrice));
        }

        //筛选出抵扣最大的sku
        /*CartSkuVO maxGoodsSku = tradeDTO.getSkuList().stream()
                .filter(CartSkuVO::getIsGreenSecurities)
                .max(Comparator.comparingDouble(CartSkuVO::getIsGreenSecuritiesPrice))
                .orElse(null);*/

        /*CartSkuVO maxGoodsSku = tradeDTO.getSkuList().stream()
                .filter(CartSkuVO::getIsGreenSecurities)
                .max(Comparator.comparingDouble(CartSkuVO::getIsGreenSecuritiesPrice))
                .orElse(null);*/

        //设置最终全返卷抵扣金额
        /*if(maxGoodsSku != null){
            if(returnActivityUser.getPrice() >= maxGoodsSku.getIsGreenSecuritiesPrice()){
                maxGoodsSku.getPriceDetailDTO().setReturnActivityUserPrice(maxGoodsSku.getIsGreenSecuritiesPrice());
            }else{
                maxGoodsSku.getPriceDetailDTO().setReturnActivityUserPrice(returnActivityUser.getPrice());
            }
            if (null != maxGoodsSku.getPromotionTypeEnum()) {
                maxGoodsSku.getPriceDetailDTO().setReturnActivityUserPrice(0D);
            }
        }*/
    }
}
